package com.leo;

import com.almasb.fxgl.app.GameApplication;
import com.almasb.fxgl.app.GameSettings;
import com.almasb.fxgl.core.math.FXGLMath;
import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.entity.SpawnData;
import com.almasb.fxgl.input.UserAction;
import com.leo.common.FruitType;
import com.leo.component.FruitComponent;
import com.leo.entity.FruitEntityFactory;
import javafx.geometry.Point2D;
import javafx.geometry.Pos;
import javafx.geometry.Rectangle2D;
import javafx.scene.control.Label;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;

import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;

/**
 * @Author: wangss
 * @CreateTime: 2023-06-18  06:26
 * @Description: 查找水果
 */
public class FindGameApp extends GameApplication {

    @Override
    protected void initSettings(GameSettings settings) {
        settings.setWidth(1000);
        settings.setHeight(600);
        settings.setAppIcon("橙子.png");
        settings.getCSSList().add("fruit.css");
    }

    @Override
    protected void initGame() {
        FXGL.getGameScene().setBackgroundColor(Color.BLACK);
        FXGL.getGameWorld().addEntityFactory(new FruitEntityFactory());

        createAllFruits();
    }

    private static void createAllFruits() {
        // 减去图片的大小，防止超出屏幕
        Rectangle2D rectangle2D = new Rectangle2D(0, 0, FXGL.getAppWidth() - 200 - 50, FXGL.getAppHeight() - 50);
        for (int i = 0; i < 20; i++) {
            FruitType type = FXGLMath.random(FruitType.values()).get();
            // 防止物体重叠
            // 查找指定范围内的实体，如果不存在才可以使用point2D
            Point2D point2D;
            while (true) {
                point2D = FXGLMath.randomPoint(rectangle2D);
                List<Entity> entitiesInRange = FXGL.getGameWorld().getEntitiesInRange(new Rectangle2D(point2D.getX(), point2D.getY(), 50, 50));
                if (entitiesInRange.isEmpty()) {
                    break;
                }
            }
            FXGL.spawn(type.toString().toLowerCase(Locale.ROOT), new SpawnData(point2D).put("type", type));
        }
    }

    @Override
    protected void initInput() {
        FXGL.getInput().addAction(new UserAction("down") {
            @Override
            protected void onActionBegin() {
                System.out.println("123");
            }
        }, KeyCode.DOWN);
    }

    @Override
    protected void initUI() {
        Label btn1 = new Label("查找苹果");
        btn1.setOnMouseClicked(event -> {
            cleanSelected();
            FXGL.getGameWorld().getEntitiesByType(FruitType.APPLE).forEach(entity -> {
                entity.getComponent(FruitComponent.class).select(true);
            });
        });
        Label btn2 = new Label("查找橘子");
        btn2.setOnMouseClicked(event -> {
            cleanSelected();
            FXGL.getGameWorld().getEntitiesByType(FruitType.ORANGE).forEach(entity -> {
                entity.getComponent(FruitComponent.class).select(true);
            });
        });
        Label btn3 = new Label("任意水果");
        btn3.setOnMouseClicked(event -> {
            cleanSelected();
            Map<Entity, FruitComponent> entitiesByComponentMapped = FXGL.getGameWorld().getEntitiesByComponentMapped(FruitComponent.class);
            if (!entitiesByComponentMapped.isEmpty()) {
                Collection<FruitComponent> values = entitiesByComponentMapped.values();
                Optional<FruitComponent> random = FXGLMath.random(values.stream().toList());
                random.ifPresent(fruitComponent -> fruitComponent.select(true));
            }
        });
        // 通过id查找
        Label btn4 = new Label("查找apple=1");
        btn4.setOnMouseClicked(event -> {
            cleanSelected();
            Optional<Entity> apple = FXGL.getGameWorld().getEntityByID("apple", 1);
            apple.ifPresent(entity -> entity.getComponent(FruitComponent.class).select(true));
        });

        // 重新生成游戏
        Label btn5 = new Label("重新生成");
        btn5.setOnMouseClicked(event -> FXGL.getGameController().startNewGame());
        Label btn6 = new Label("重新生成");
        btn6.setOnMouseClicked(event -> {
            cleanSelected();
            FXGL.getGameWorld().removeEntities(FXGL.getGameWorld().getEntitiesCopy());
            createAllFruits();
        });
        VBox box = new VBox(20, btn1, btn2, btn3, btn4, btn5, btn6);
        box.setPrefSize(200, 600);
        box.getStyleClass().add("choose-box");
        box.setAlignment(Pos.CENTER);
        FXGL.addUINode(box, 800, 0);
    }

    /**
     * 清除选中状态，方式一
     */
    public void cleanSelected() {
        FXGL.getGameWorld().getEntitiesByComponentMapped(FruitComponent.class).forEach((entity, component) -> {
            component.select(false);
        });
    }

    public static void main(String[] args) {
        launch(args);
    }
}
